package com.pengpeng.android.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.pengpeng.android.entity.MapEntity;

public class MapDao extends SuperDao{
	
	
	private void createTable()
	{
		String sqlstr="create table map(";
		sqlstr+="maptype varchar(10) not null,";
		sqlstr+="initloation varchar(40),null";
		sqlstr+="zoomlevel char(3) null,";
		sqlstr+="viewmode char(2) null,";
		sqlstr+="iftraffic char(2) null,";
	    sqlstr+="routemode char(2) null,";
	    sqlstr+="constraint PK_MAP primary key clustered (maptype)";
	    sqlstr+=");";
		sqlitedb.execSQL(sqlstr);
	}//createTable
	
    private void insert(MapEntity mapentity)
    {
    	String sqlstr="insert into map(";
    	sqlstr+="maptype,initloation,zoomlevel,viewmode,iftraffic,routemode) ";
    	sqlstr+="values('"+mapentity.getMaptype()+"','";
    	sqlstr+=mapentity.getInitlocation()+"','";
    	sqlstr+=mapentity.getZoomlevel()+"','";
    	sqlstr+=mapentity.getViewmode()+"','";
    	sqlstr+=mapentity.getIftraffic()+"','";
    	sqlstr+=mapentity.getRoutemode()+"')";
		sqlitedb.execSQL(sqlstr);
    }//insert
    
    private void delete()
    {
    	//map的记录不能删除
    }//delete
    
    public void update(MapEntity mapentity)
    {
    	String sqlstr="update map set ";
    	sqlstr+="maptype='"+mapentity.getMaptype()+"',";
    	sqlstr+="initloation='"+mapentity.getInitlocation()+"',";
    	sqlstr+="zoomlevel='"+mapentity.getZoomlevel()+"',";
    	sqlstr+="viewmode='"+mapentity.getViewmode()+"',";
    	sqlstr+="iftraffic='"+mapentity.getIftraffic()+"',";
    	sqlstr+="routemode='"+mapentity.getRoutemode()+"'";
		sqlitedb.execSQL(sqlstr);

    	
    }//update
    
    public MapEntity query()
    {
    	String sqlstr="";
		Cursor cursor=null;
		int result=0;
		MapEntity mapentity=null;
		sqlstr="select maptype,initloation,zoomlevel,viewmode,";
		sqlstr+="iftraffic,routemode from map";
		cursor=sqlitedb.rawQuery(sqlstr, null);
		cursor.moveToFirst();
		if(!cursor.isAfterLast())
		{
			String maptype=cursor.getString(0);
			String initloacation=cursor.getString(1);
			String zoomlevel=cursor.getString(2);
			String viewmode=cursor.getString(3);
			String iftraffic=cursor.getString(4);
			String routemode=cursor.getString(5);
			
			mapentity=new MapEntity();
			mapentity.setMaptype(maptype);
			mapentity.setInitlocation(initloacation);
			mapentity.setZoomlevel(zoomlevel);
			mapentity.setViewmode(viewmode);
			mapentity.setIftraffic(iftraffic);
			mapentity.setRoutemode(routemode);
						
		}
		cursor.close();
        return mapentity;
    }//query
    
    public void close()
    {
    	closeDatabase();
    }//close
    
    public MapDao()
    {
    	openDatabase();
    	if(!existTable("map"))
    	{
    		createTable();
    		MapEntity mapentity=new MapEntity();
    		mapentity.setIftraffic("0");
    		mapentity.setInitlocation("39.915,116.404");
    		mapentity.setMaptype("baidu");
    		mapentity.setRoutemode("0");
    		mapentity.setViewmode("1");
    		mapentity.setZoomlevel("6");
    		insert(mapentity);
    	}
    }//MapDao
  
}
